/**
  **************************************************************************
  * @file     at32f403a_407_int.c
  * @version  v2.0.0
  * @date     2020-11-02
  * @brief    main interrupt service routines.
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

/* includes ------------------------------------------------------------------*/
#include "at32f403a_407_int.h"
#include "i2c_application.h"

extern i2c_handle_type hi2cx;



/** @addtogroup AT32F403A_periph_examples
  * @{
  */

/** @addtogroup 403A_I2C_communication_int
  * @{
  */

/**
  * @brief  this function handles nmi exception.
  * @param  none
  * @retval none
  */
void NMI_Handler(void)
{
}

/**
  * @brief  this function handles hard fault exception.
  * @param  none
  * @retval none
  */
void HardFault_Handler(void)
{
  /* go to infinite loop when hard fault exception occurs */
  while(1)
  {
  }
}

/**
  * @brief  this function handles memory manage exception.
  * @param  none
  * @retval none
  */
void MemManage_Handler(void)
{
  /* go to infinite loop when memory manage exception occurs */
  while(1)
  {
  }
}

/**
  * @brief  this function handles bus fault exception.
  * @param  none
  * @retval none
  */
void BusFault_Handler(void)
{
  /* go to infinite loop when bus fault exception occurs */
  while(1)
  {
  }
}

/**
  * @brief  this function handles usage fault exception.
  * @param  none
  * @retval none
  */
void UsageFault_Handler(void)
{
  /* go to infinite loop when usage fault exception occurs */
  while(1)
  {
  }
}

/**
  * @brief  this function handles svcall exception.
  * @param  none
  * @retval none
  */
void SVC_Handler(void)
{
}

/**
  * @brief  this function handles debug monitor exception.
  * @param  none
  * @retval none
  */
void DebugMon_Handler(void)
{
}

/**
  * @brief  this function handles pendsv_handler exception.
  * @param  none
  * @retval none
  */
void PendSV_Handler(void)
{
}

/**
  * @brief  this function handles systick handler.
  * @param  none
  * @retval none
  */
void SysTick_Handler(void)
{
}

/**
  * @brief  this function handles i2c event interrupt request.
  * @param  none
  * @retval none
  */
void I2C2_EVT_IRQHandler(void)
{
	if (I2C2->sts1_bit.addr7f ){

		uint32_t read_sts2 = I2C2->sts2_bit.dirf;

	} else if(I2C2->sts1_bit.rdbf){

    	if(hi2cx.pcount)
		{
			/* read data */
    		(*hi2cx.pbuff++) = (uint8_t)hi2cx.i2cx->dt;
    		hi2cx.pcount--;
		}
	}

  if(I2C2->sts1_bit.stopf){
    I2C2->ctrl1_bit.genstop = 0;
    hi2cx.pcount = 7;
    hi2cx.pbuff  = rx_buf;
  }

  i2c_command_handler(&rx_buf);
}


/**
  * @brief  this function handles i2c error interrupt request.
  * @param  none
  * @retval none
  */
void I2C2_ERR_IRQHandler(void)
{
  i2c_err_irq_handler(&hi2cx);
}

void TMR6_GLOBAL_IRQHandler(void)
{
  TMR6->ists_bit.ovfif = 0;
  if(mtr_mode == PID_REGULATOR){
	  CalcPid(motorA_ptr);
	  CalcPid(motorB_ptr);
  } else if(mtr_mode == DIRECT_MODE){
	  Parrot_find(motorA_ptr);
	  Parrot_find(motorB_ptr);
	  SetPWMbyReg(motorA_ptr);
	  SetPWMbyReg(motorB_ptr);
  } else if(mtr_mode == DIRECTION_STOP){
	  motor_stop(motorA_ptr, motorB_ptr);
  }
}

void USART2_IRQHandler(void)
{
  usart2_tx_rx_handler();
}


void EXINT15_10_IRQHandler(void)
{
	// Проверка, что прерывание вызвано именно линией 13
	if (EXINT->intsts & EXINT_LINE_13) {
		OdometrProcess(motorA_ptr);
		EXINT->intsts = EXINT_LINE_13;
	}
}

void EXINT0_IRQHandler(void)
{
	if (EXINT->intsts & EXINT_LINE_0) {
		OdometrProcess(motorB_ptr);
		EXINT->intsts = EXINT_LINE_0;
	}
}

//// Обработчик прерывания по переполнению таймера
//void TMR5_GLOBAL_IRQHandler(void) {
//	tmr5_handler();
//}



/**
  * @}
  */

/**
  * @}
  */
